﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BizVal.Contracts
{
    // ToDo: Convert to generic type.
    public class Interval
    {
        public decimal LowerValue { get; set; }

        public decimal HigherValue { get; set; }

        public static Interval operator +(decimal a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(b.HigherValue + a, 4),
                LowerValue = Math.Round(b.LowerValue + a, 4)
            };
        }

        public static Interval operator +(Interval a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(b.HigherValue + a.HigherValue, 4),
                LowerValue = Math.Round(b.LowerValue + a.LowerValue, 4)
            };
        }

        public static Interval operator +(Interval a, decimal b)
        {
            return b + a;
        }

        public static Interval operator -(Interval a, decimal b)
        {
            return b - a;
        }

        public static Interval operator *(Interval a, decimal b)
        {
            return b * a;
        }


        public static Interval operator -(decimal a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(b.HigherValue - a, 4),
                LowerValue = Math.Round(b.LowerValue - a, 4)
            };
        }

        public static Interval operator *(decimal a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(b.HigherValue * a, 4),
                LowerValue = Math.Round(b.LowerValue * a, 4)
            };
        }

        public static Interval operator *(Interval a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(b.HigherValue * a.HigherValue, 4),
                LowerValue = Math.Round(b.LowerValue * a.LowerValue, 4)
            };
        }

        public static Interval operator /(Interval a, decimal b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(a.HigherValue / b, 4),
                LowerValue = Math.Round(a.LowerValue / b, 4)
            };
        }

        public static Interval operator /(decimal a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(a / b.HigherValue, 4),
                LowerValue = Math.Round(a / b.LowerValue, 4)
            };
        }

        public static Interval operator /(Interval a, Interval b)
        {
            return new Interval()
            {
                HigherValue = Math.Round(a.HigherValue / b.HigherValue, 4),
                LowerValue = Math.Round(a.LowerValue / b.LowerValue, 4)
            };
        }

        public static Interval operator ^(Interval a, int b)
        {
            return new Interval()
            {
                HigherValue = Convert.ToDecimal(Math.Pow((double)a.HigherValue, (double)b)),
                LowerValue = Convert.ToDecimal(Math.Pow((double)a.LowerValue, (double)b))
            };
        }

        public static Interval operator %(Interval a, Interval b)
        {
            return new Interval()
            {
                HigherValue = a.HigherValue > b.HigherValue ? a.HigherValue : b.HigherValue,
                LowerValue = a.LowerValue < b.LowerValue ? a.LowerValue : b.LowerValue
            };
        }


    }
}
